package com.miplus.ccrm.core.service.conf;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.miplus.ccrm.common.CmdResVo;
import com.miplus.ccrm.common.EnumRCode;
import com.miplus.ccrm.common.JPubUtils;
import com.miplus.ccrm.common.ServiceException;
import com.miplus.ccrm.core.controller.vo.conf.IouProductEditReqVo;
import com.miplus.ccrm.core.controller.vo.conf.IouProductPageReqVo;
import com.miplus.ccrm.core.controller.vo.conf.IouProductPageResVo;
import com.miplus.ccrm.core.controller.vo.conf.IouSelectProductResVo;
import com.miplus.ccrm.core.pojo.FiCreditcases;
import com.miplus.ccrm.core.pojo.IouPorductApi;
import com.miplus.ccrm.core.pojo.IouProduct;
import com.miplus.ccrm.core.service.conf.IouProductService;
import com.miplus.ccrm.core.mapper.IouProductMapper;
import com.miplus.ccrm.core.service.crd.FiCreditcasesService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
* @author Yaohui
* @description 针对表【iou_product】的数据库操作Service实现
* @createDate 2024-11-04 09:45:42
*/
@Service
public class IouProductServiceImpl extends MPJBaseServiceImpl<IouProductMapper, IouProduct>
    implements IouProductService{
    @Resource
    IouPorductApiService iouPorductApiService;

    @Resource
    FiCreditcasesService fiCreditcasesService;

    @Override
    public IPage<IouProductPageResVo> productPage(IouProductPageReqVo reqVo) {
        return selectJoinListPage(new Page<>(reqVo.getPageNum(),reqVo.getPageSize()),
                IouProductPageResVo.class,
                new MPJLambdaWrapper<IouProduct>()
                        .selectAll(IouProduct.class)
                        .selectAs("fun_getnames('PRODUCT.SOURCE',t.source)",IouProductPageResVo::getSourceDesc)
                        .selectAs("fun_getnames('PRODUCT.TYPE',t.product_type)",IouProductPageResVo::getProductTypeDesc)
                        .selectAs("fun_getnames('PRODUCT.CATALOG',t.product_catalog)",IouProductPageResVo::getProductCatalogDesc)
                        .selectAs("fun_getnames('PRODUCT.STATUS',t.status)",IouProductPageResVo::getStatusDesc)
                        .eq(JPubUtils.IsNotEmpty(reqVo.getProductId()),IouProduct::getProductId,reqVo.getProductId())
                        .eq(JPubUtils.IsNotEmpty(reqVo.getProductName()),IouProduct::getProductName,reqVo.getProductName())
                        .eq(JPubUtils.IsNotEmpty(reqVo.getMerchantId()),IouProduct::getMerchantId,reqVo.getMerchantId())
                        .eq(JPubUtils.IsNotEmpty(reqVo.getMerchantName()),IouProduct::getMerchantName,reqVo.getMerchantName())
                        .eq(JPubUtils.IsNotEmpty(reqVo.getProductType()),IouProduct::getProductType,reqVo.getProductType())
                        .eq(JPubUtils.IsNotEmpty(reqVo.getProductCatalog()),IouProduct::getProductCatalog,reqVo.getProductCatalog())
                        .eq(JPubUtils.IsNotEmpty(reqVo.getStatus()),IouProduct::getStatus,reqVo.getStatus())
                        .eq(JPubUtils.IsNotEmpty(reqVo.getSource()),IouProduct::getSource,reqVo.getSource())
        ).convert(x->{
            x.setProductApiList(getApiList(x.getId()));
            return x;
        });
    }

    @Override
    public List<IouSelectProductResVo> selectProduct() {

        return baseMapper.selectProduct();
    }
    @Override
    public List<IouSelectProductResVo> selectMerchant() {

        return baseMapper.selectMerchant();
    }

    @Override
    @Transactional
    public CmdResVo productInsert(String $usrid,IouProductEditReqVo reqVo) {
        IouProduct iouProduct=new IouProduct();
        BeanUtils.copyProperties(reqVo,iouProduct);
        iouProduct.setCrdt(new Date());
        iouProduct.setCrusr($usrid);
        iouProduct.setStatus(1);
        int ret = baseMapper.insert(iouProduct);
        iouPorductApiService.remove( new LambdaQueryWrapper<IouPorductApi>()
                .eq(IouPorductApi::getProductId,iouProduct.getProductId())
                .eq(IouPorductApi::getMerchantId,iouProduct.getMerchantId()));
//        if(reqVo.getProductApiList() != null ) {
//            for (String s : reqVo.getProductApiList())
//            {
//                IouPorductApi iouPorductApi = new IouPorductApi();
//                iouPorductApi.setProductId(iouProduct.getProductId());
//                iouPorductApi.setApiId(s);
//                iouPorductApiService.save(iouPorductApi);
//            }
//        }
        iouPorductApiService.updateProductApi(reqVo.getProductApiList(),iouProduct.getProductId(),reqVo.getMerchantId());
        return new CmdResVo(ret,iouProduct.getId().toString());
    }

    @Override
    @Transactional
    public CmdResVo productEdit(String $usrid,IouProductEditReqVo reqVo) {

        IouProduct iouProduct=new IouProduct();
        BeanUtils.copyProperties(reqVo,iouProduct);
        iouProduct.setMddt(new Date());
        iouProduct.setMdusr($usrid);
        iouPorductApiService.remove( new LambdaQueryWrapper<IouPorductApi>().eq(IouPorductApi::getProductId,iouProduct.getProductId())
                .eq(IouPorductApi::getMerchantId,iouProduct.getMerchantId()));
        int ret =baseMapper.updateById(iouProduct);
        iouPorductApiService.updateProductApi(reqVo.getProductApiList(),reqVo.getProductId(),reqVo.getMerchantId());
        return new CmdResVo(ret,iouProduct.getId().toString());
    }
    @Override
    public CmdResVo productDelete(IouProductEditReqVo reqVo) {

        IouProduct iouProduct = getById(reqVo.getId());
        if(iouProduct==null){
            throw new ServiceException(498, "产品信息错误，删除失败");
        }
        long count = fiCreditcasesService.count(new MPJLambdaWrapper<>(FiCreditcases.class)
                .eq(FiCreditcases::getProductId, iouProduct.getProductId()));
        if(count>0){
            throw new ServiceException(498, "产品已绑定案件信息，删除失败");
        }
        removeById(reqVo.getId());
        return new CmdResVo(1);
    }
    private List<String> getApiList(Long id)
    {
        return selectJoinList(
                String.class,
                new MPJLambdaWrapper<IouProduct>()
                        .leftJoin( "iou_porduct_api ipa on ipa.product_id = t.product_id and ifnull(ipa.merchant_id,'') = ifnull(t.merchant_id,'')")
                        .select("ipa.api_id")
                        .eq(IouProduct::getId,id)
        );

    }
}




